﻿using System;
using System.Linq;
using eCommerce.core;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;

namespace eCommerce.web.Controls.Product
{
    public partial class List : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var categoryName = Utils.GetIdFromRequestString(Utils.Name, string.Empty);
                var parentcategoryName = Utils.GetIdFromRequestString(Utils.Parent_Name, string.Empty);

                if (!string.IsNullOrEmpty(parentcategoryName))
                {
                    categoryName = parentcategoryName + "/" + categoryName;
                }

                var o = CategoryBo.GetByUrl(categoryName);
                if (o != null)
                {
                    var lstId = string.Empty;
                    var lstCategory = CategoryBo.GetListByParentId((int)CategoryType.Product, o.CategoryId, (int) CategoryType.All);
                    lstId += o.CategoryId + ", ";
                    lstId = lstCategory.Aggregate(lstId, (current, category) => current + string.Format("{0}, ", category.CategoryId));
                    if (lstId.Length > 0)
                        lstId = lstId.Substring(0, lstId.Length - 2);

                    ltScript.Text =
                        string.Format(
                            "<script type=\"text/javascript\"> " +
                                "eCommerce.LstCategory = [{0}]; " +
                                "getListFilterProduct('{0}', '', eCommerce.Sort, eCommerce.PageIndex, eCommerce.PageSize);" +
                                "</script>",   lstId);                    
                }
                if(categoryName== "san-pham-moi")
                {
                    
                    ltScript.Text =
                        string.Format(
                            "<script type=\"text/javascript\"> " +
                                "eCommerce.LstCategory = [{0}]; " +
                                "getListFilterProduct('{0}', '', eCommerce.Sort, eCommerce.PageIndex, eCommerce.PageSize);" +
                                "</script>",   "0111" );          
                }
                if (categoryName == "khuyen-mai")
                {

                    ltScript.Text =
                        string.Format(
                            "<script type=\"text/javascript\"> " +
                                "eCommerce.LstCategory = [{0}]; " +
                                "getListFilterProduct('{0}', '', eCommerce.Sort, eCommerce.PageIndex, eCommerce.PageSize);" +
                                "</script>", "0112");
                }
            }            
        }
    }
}